package org.mybatis.generator.ext.comment;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Set;
import java.util.TreeSet;

import org.apache.commons.lang.StringUtils;
import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.dom.java.CompilationUnit;
import org.mybatis.generator.api.dom.java.Field;
import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
import org.mybatis.generator.api.dom.java.InnerClass;
import org.mybatis.generator.api.dom.java.InnerEnum;
import org.mybatis.generator.api.dom.java.Method;
import org.mybatis.generator.api.dom.xml.TextElement;
import org.mybatis.generator.api.dom.xml.XmlElement;
import org.mybatis.generator.internal.DefaultCommentGenerator;
/**
 * @author ∞。1
 * @Description 中文注释  
 * @date 2017年10月6日20:20:38
 *
 */
public class ChineseComment extends DefaultCommentGenerator {

   
    @Override
    public void addFieldComment(Field field, IntrospectedTable introspectedTable) { 
    }
    
    @Override
    public void addGeneralMethodComment(Method method, IntrospectedTable introspectedTable) {
    	

    }
    

    @Override
	public void addRootComment(XmlElement rootElement) { 	}
	@Override
	public void addClassComment(InnerClass innerClass, IntrospectedTable introspectedTable) {	
		
	}
	@Override
	public void addEnumComment(InnerEnum innerEnum, IntrospectedTable introspectedTable) {	}
	@Override
	public void addClassComment(InnerClass innerClass, IntrospectedTable introspectedTable, boolean markAsDoNotDelete) {
	}
	
	
	
	@Override
	public void addJavaFileComment(CompilationUnit compilationUnit) {
	        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	        compilationUnit.addFileCommentLine("/**");
	        compilationUnit.addFileCommentLine(" *" + sdf.format(new Date()));
	        compilationUnit.addFileCommentLine(" */");
	        String self=compilationUnit.getType().getFullyQualifiedName();
	        if (self.endsWith("Mapper")) {
	        		//给Mapper导入RowBounds包
	        	   Set<FullyQualifiedJavaType> importedTypes = new TreeSet<FullyQualifiedJavaType>();
	               importedTypes.add(new FullyQualifiedJavaType("org.apache.ibatis.session.RowBounds"));
	               compilationUnit.addImportedTypes(importedTypes);
			}
	   }
	
	@Override
    public void addGetterComment(Method method, IntrospectedTable introspectedTable,IntrospectedColumn introspectedColumn) {
    	method.addJavaDocLine("/**");
    	method.addJavaDocLine("* 获取:"+getColumDefinition(introspectedColumn));
    	method.addJavaDocLine("**/");
    	if(introspectedColumn.getFullyQualifiedJavaType().getShortName().equals("Date")){
    		method.addAnnotation("@com.alibaba.fastjson.annotation.JSONField(format=\"yyyy-MM-dd HH:mm:ss\")");
    	}
    }
    @Override
    public void addSetterComment(Method method, IntrospectedTable introspectedTable,IntrospectedColumn introspectedColumn) {
    	method.addJavaDocLine("/**");
    	method.addJavaDocLine("* 设置:"+getColumDefinition(introspectedColumn));
    	method.addJavaDocLine("**/");
    }
    
    @Override
    public void addComment(XmlElement xmlElement) {
    	String methodId=xmlElement.getAttributes().get(0).getValue();
    	String comment=getMethodComment(methodId);
    	if(!StringUtils.isBlank(comment) &&!StringUtils.isEmpty(comment)) {
	        xmlElement.addElement(new TextElement("<!--"+comment+"-->")); //$NON-NLS-1$
    	}
    }

    @Override
    public void addFieldComment(Field field, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) {
//    	String remark = getColumDefinition(introspectedColumn);
    	field.addJavaDocLine("/**");
    	field.addJavaDocLine("* "+getColumDefinition(introspectedColumn));
    	field.addJavaDocLine("* 字段:"+introspectedColumn.getActualColumnName());
    	field.addJavaDocLine("**/");
//    	field.addAnnotation("@Column(name=\""+field.getName()+"\",columnDefinition=\""+remark+"\")");
    }

    
	private String getColumDefinition(IntrospectedColumn introspectedColumn) {
		String remark=introspectedColumn.getRemarks();
    	if (StringUtils.isBlank(remark)||StringUtils.isEmpty(remark)) {
			remark=introspectedColumn.getActualColumnName();
		}
		return remark;
	}

	private String getMethodComment(String method_name) {
		if ("deleteByPrimaryKey".equals(method_name)) {
            return "*  根据主键删除数据库的记录";
        } else if ("insert".equals(method_name)) {
            return "*  插入数据库记录";
        } else if ("selectByPrimaryKey".equals(method_name)) {
            return "*  根据主键获取一短信数量据库记录";
        } else if ("updateByPrimaryKey".equals(method_name)) {
            return "*  根据主键来更新数据库记录";
        } else if ("selectAll".equals(method_name)) {
            return "*  获取全部数据库记录";
        } else if ("countByExample".equals(method_name)) {
            return "*  根据条件数据库记录短信数量";
        }else if ("deleteByExample".equals(method_name)) {
            return "*  根据条件删除数据库记录";
        }else if ("insertSelective".equals(method_name)) {
            return "*  插入一条记录到数据库";
        }else if ("selectByExample".equals(method_name)) {
            return "*  根据条件查询数据库记录";
        }else if ("updateByExample".equals(method_name)) {
            return "*  更新数据库记录";
        }else if ("updateByExampleSelective".equals(method_name)) {
            return "*  更新数据库记录";
		}else if ("updateByPrimaryKey".equals(method_name)) {
            return "*  更新数据库记录";
		}
		return "";
	}

}